package com.pig4cloud.pig.sthcloud.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.HasPermission;
import com.pig4cloud.pig.sthcloud.entity.GpggaData;
import com.pig4cloud.pig.sthcloud.entity.SthcloudPpkSloveTaskEntity;
import com.pig4cloud.pig.sthcloud.service.SthcloudPpkSloveTaskService;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * sthcloud表
 *
 * @author pig
 * @date 2025-07-02 16:42:16
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sthcloudPpkSloveTask" )
@Tag(description = "sthcloudPpkSloveTask" , name = "sthcloud表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SthcloudPpkSloveTaskController {

    private final  SthcloudPpkSloveTaskService sthcloudPpkSloveTaskService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param sthcloudPpkSloveTask sthcloud表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @HasPermission("sthcloud_sthcloudPpkSloveTask_view")
    public R getSthcloudPpkSloveTaskPage(@ParameterObject Page page, @ParameterObject SthcloudPpkSloveTaskEntity sthcloudPpkSloveTask) {
        LambdaQueryWrapper<SthcloudPpkSloveTaskEntity> wrapper = Wrappers.lambdaQuery();

		// 如果传入了有效的ID，则使用精确匹配
		if (sthcloudPpkSloveTask != null && sthcloudPpkSloveTask.getId() != null && sthcloudPpkSloveTask.getId() > 0) {
			wrapper.eq(SthcloudPpkSloveTaskEntity::getId, sthcloudPpkSloveTask.getId());
		}


		return R.ok(sthcloudPpkSloveTaskService.page(page, wrapper));
    }


    /**
     * 通过条件查询sthcloud表
     * @param sthcloudPpkSloveTask 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @HasPermission("sthcloud_sthcloudPpkSloveTask_view")
    public R getDetails(@ParameterObject SthcloudPpkSloveTaskEntity sthcloudPpkSloveTask) {
        return R.ok(sthcloudPpkSloveTaskService.list(Wrappers.query(sthcloudPpkSloveTask)));
    }

    /**
     * 通过ID获取GPGGA数据
     * @param id 数据ID
     * @return R 包含GPGGA数据列表的统一响应对象
     */
    @Operation(summary = "通过ID获取GPGGA数据", description = "根据ID查询对应的GPGGA定位数据")
    @GetMapping("/data")
    @HasPermission("sthcloud_sthcloudPpkSloveTask_view")
    public R getGpggaData(@Parameter(description = "数据ID", required = true) @RequestParam Integer id) {
          return sthcloudPpkSloveTaskService.listGpggaDataById(id);
    }

	@Operation(summary = "上传并解析GPGGA文件")
	@PostMapping("/upload")
	@HasPermission("sthcloud_sthcloudPpkSloveTask_view")
	public R uploadGpggaFile(
			@Parameter(description = "GPGGA文件") @RequestParam("file") MultipartFile file
	) {
		return sthcloudPpkSloveTaskService.parseAndSaveGpggaFile(file);
	}

    /**
     * 新增sthcloud表
     * @param sthcloudPpkSloveTask sthcloud表
     * @return R
     */
    @Operation(summary = "新增sthcloud表" , description = "新增sthcloud表" )
    @SysLog("新增sthcloud表" )
    @PostMapping
    @HasPermission("sthcloud_sthcloudPpkSloveTask_add")
    public R save(@RequestBody SthcloudPpkSloveTaskEntity sthcloudPpkSloveTask) {
        return R.ok(sthcloudPpkSloveTaskService.save(sthcloudPpkSloveTask));
    }

    /**
     * 修改sthcloud表
     * @param sthcloudPpkSloveTask sthcloud表
     * @return R
     */
    @Operation(summary = "修改sthcloud表" , description = "修改sthcloud表" )
    @SysLog("修改sthcloud表" )
    @PutMapping
    @HasPermission("sthcloud_sthcloudPpkSloveTask_edit")
    public R updateById(@RequestBody SthcloudPpkSloveTaskEntity sthcloudPpkSloveTask) {
        return R.ok(sthcloudPpkSloveTaskService.updateById(sthcloudPpkSloveTask));
    }

    /**
     * 通过id删除sthcloud表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除sthcloud表" , description = "通过id删除sthcloud表" )
    @SysLog("通过id删除sthcloud表" )
    @DeleteMapping
    @HasPermission("sthcloud_sthcloudPpkSloveTask_del")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(sthcloudPpkSloveTaskService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param sthcloudPpkSloveTask 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @HasPermission("sthcloud_sthcloudPpkSloveTask_export")
    public List<SthcloudPpkSloveTaskEntity> exportExcel(SthcloudPpkSloveTaskEntity sthcloudPpkSloveTask,Integer[] ids) {
        return sthcloudPpkSloveTaskService.list(Wrappers.lambdaQuery(sthcloudPpkSloveTask).in(ArrayUtil.isNotEmpty(ids), SthcloudPpkSloveTaskEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param sthcloudPpkSloveTaskList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @HasPermission("sthcloud_sthcloudPpkSloveTask_export")
    public R importExcel(@RequestExcel List<SthcloudPpkSloveTaskEntity> sthcloudPpkSloveTaskList, BindingResult bindingResult) {
        return R.ok(sthcloudPpkSloveTaskService.saveBatch(sthcloudPpkSloveTaskList));
    }
}
